import { Component, Input, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { MyModalHelper } from '../../../core/my-modal.helper';
import { CenterService } from '../../../core/routes-services/center.service';
import { myValidNull } from '../../../core/util/string-utils';
import { OtcCreditViewComponent } from './otc-credit-view/otc-credit-view.component';
import { OtcBillService } from '../../../core/routes-services/otc-bill.service';
import { OtcConvertService } from '../../../core/routes-services/otc-convert.service';
import { OtcBillEditComponent } from '../../otc-cus/otc-bill-list/otc-bill-edit/otc-bill-edit.component';
import { OtcConvertViewComponent } from '../../otc-convert/otc-convert-list/otc-convert-view/otc-convert-view.component';
import { OTC_RECORD_TYPE } from '../../../constant/dictionary';

@Component({
  selector: 'app-otc-credit-record',
  templateUrl: './otc-credit-record.component.html',
})
export class OtcCreditRecordComponent implements OnInit {

  @Input() customerId: any;
  @Input() centerId: any;
  @Input() filterCenterNull:string;//搜索全部用户积分 _filter_isnull_centerId  或全部运营中心积分 _filter_isnotnull_centerId

  isLoading = false;
  keyword: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  operateType:any;
  otcRecordType:any [] =OTC_RECORD_TYPE;

  constructor(private message: NzMessageService,
              private centerService: CenterService,
              private otcBillService: OtcBillService,
              private otcConvertService: OtcConvertService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }

  initPage() {
    this.isLoading = true;
    this.centerService.creditPagination(this.page, this.keyword, this.operateType,this.customerId, this.centerId, this.filterCenterNull).then((res) => {
      console.log(res);
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  seeDetail = (entity) => {
    // 积分记录明细 operate_type= 1|2|3- 票据id, 4|7-转移记录id 5|6-兑换记录) 8充值记录 9|10 核销
    //票据 兑换
    if (entity.operateType <= 3) {
        this.otcBillService.detail(entity.relateId).then((res) => {
          if (res.status.toString().startsWith('20')) {
            this.modalHelper.open(OtcBillEditComponent, { entity:  res.data }, 660, {
              nzTitle: '积分明细详情',
            }).subscribe(result => {
              // if (result) {
              //   temp ? this.search() : this.changePageNo(this.page.number);
              // }
            }, (error) => {
              console.error('弹窗回调出错：', error);
            });
          } else {
            this.message.warning(res.message);
          }
          this.isLoading = false;
        }, error => {
          this.message.error(error.message ? error.message : error.error);
          this.isLoading = false;
        });
    } else if (entity.operateType == 5 || entity.operateType == 6 || entity.operateType == 11) {
      this.otcConvertService.detail(entity.relateId).then((res) => {
        if (res.status.toString().startsWith('20')) {
          this.modalHelper.open(OtcConvertViewComponent, { entity:  res.data }, 800, {
            nzTitle: '积分明细详情',
          }).subscribe(result => {
            // if (result) {
            //   temp ? this.search() : this.changePageNo(this.page.number);
            // }
          }, (error) => {
            console.error('弹窗回调出错：', error);
          });
        } else {
          this.message.warning(res.message);
        }
        this.isLoading = false;
      }, error => {
        this.message.error(error.message ? error.message : error.error);
        this.isLoading = false;
      });
    } else {
      let temp: boolean;
      temp = myValidNull(entity.id);
      const bak = {};
      Object.assign(bak, entity);
      this.modalHelper.open(OtcCreditViewComponent, { entity: bak }, 800, {
        nzTitle: '积分明细详情',
      }).subscribe(result => {
        if (result) {
          temp ? this.search() : this.changePageNo(this.page.number);
        }
      }, (error) => {
        console.error('弹窗回调出错：', error);
      });
    }
  };

}
